﻿<%@ WebHandler Language="C#" Class="hesuan_kucun" %>

using System;
using System.Web;
using KeaoBase;
using ServiceStack.Redis;
using System.Configuration;
using System.Collections.Generic;
using System.Data;

public class hesuan_kucun : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid =string.Format("{0}",context.Request.QueryString["uid"]);
            string kid =string.Format("{0}", context.Request.QueryString["kid"]);
            string requesType =string.Format("{0}",context.Request.QueryString["requestType"]);
            string gengxin =string.Format("{0}" ,context.Request.QueryString["gengxin"]);
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("[]");
                return;
            }
            string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid, "cache").Replace(kid, "cache"));
            if (string.IsNullOrEmpty(gengxin))
            {
                using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
                {
                    if (redis != null)
                    {
                        if (redis.ContainsKey(key))
                        {
                            context.Response.Write(redis.GetValue(key));
                            return;
                        }
                    }
                }
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                switch (requesType)
                {
                    case "1":
                        string zhandian = string.Format("{0}", context.Request.QueryString["zhandian"]);
                        string riqi = string.Format("{0}", context.Request.QueryString["kaishi"]);

                        string createCondition = m_DB.CreateAndCondition("riqi", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", riqi), string.Format("{0} 23:59:59", riqi), SearchConditionType.between);
                        if (!string.IsNullOrEmpty(zhandian) && zhandian != "—全部—")
                        {
                            createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhandian, "", SearchConditionType.等于);
                        }
                        Dictionary<string, string> orderBys = new Dictionary<string, string>();
                        orderBys.Add("wuliaomingcheng", "asc");
                        orderBys.Add("xinghaomingcheng", "asc");
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_kucun", createCondition, orderBys);
                        if (Dt.Rows.Count > 0)
                        {
                            string responseJson = KeaoUtils.Dt2Json(Dt);
                            if (string.IsNullOrEmpty(gengxin))
                            {
                                using (IRedisClient redis = RedisClientPool.GetRedisClient())
                                {
                                    if (redis != null)
                                    {
                                        redis.SetValue(key, responseJson, TimeSpan.FromMinutes(10.0));
                                    }
                                }
                            }
                            context.Response.Write(responseJson);
                        }
                        else
                        {
                            context.Response.Write("[]");
                        }
                        break;
                    case "2":
                        string tiaochaID = string.Format("{0}", context.Request.QueryString["tiaochaID"]);
                        string tiaocha = string.Format("{0}", context.Request.QueryString["tiaocha"]);
                        if (KeaoUtils.isNumber(tiaocha))
                        {
                            object param1 = m_DB.CreateParamter("@tiaocha", tiaocha, false);
                            object param2 = m_DB.CreateParamter("@tiaochaid", tiaochaID, false);
                            object returnValue = m_DB.ExecuteProcedure("P_KuCunTiaoCha", new object[] { param1, param2 });
                            context.Response.Write("OK");
                        }
                        else
                        {
                            context.Response.Write("调差数据必须为整数。");
                        }
                        break;
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("库存查询异常。{0}", err.Message));
            context.Response.Write("请重试...");
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}